<?php

namespace app\beautiful\controller\admin;

use app\BaseController;
use app\common\model\GoodsSpecModel;


class Goodsspec extends BaseController{

    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $where=[];
        $shop_id=$this->request->shopId;
        $where['shop_id']=$shop_id;
        $order['a.id']='desc';
        $res_data=GoodsSpecModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function info(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $where['id']=$id;
        $res_data=GoodsSpecModel::info($where);
        return $this->success($res_data);
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=GoodsSpecModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $up['is_delete']=$is_delete;
        $res=GoodsSpecModel::up($up,$where);
        if(!$res){
            return $this->error('删除失败');
        }
        return $this->success();
    }
    public function edit(){
        $shop_id=$this->request->shopId;
        $id = $this->request->param('id',0);
        $name = $this->request->param('name',null);
        $spec_text=$this->request->param('spec_text',null);
        if($id==0){
            return $this->error('id错误');
        }
        $GoodsSpecModel= new GoodsSpecModel();
        $all_info=$GoodsSpecModel->where(['id'=>$id])->find();
        if(empty($all_info)) return $this->error('数据不存在');
        $s=[];
        $s[]=['name','=',$name];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['id','<>',$id];
        $s[]=['is_delete','=',0];
        $info=$GoodsSpecModel->where($s)->find();
        if(!empty($info)) return $this->error('数据重复');
        $up=[];
        if($name!=null) $up['name']=$name;
        if($spec_text!=null) {
            foreach ($spec_text as $k=>&$va){
                $va['sort']=$k+1;
            }
            $up['spec_text']=json_encode($spec_text);
        };
        $where['id']=$id;
        $res_data=GoodsSpecModel::up($up,$where);
        return $this->success($res_data);
    }
    public function add(){
        $shop_id=$this->request->shopId;
        $name = $this->request->param('name',null);
        $spec_text=$this->request->param('spec_text',null);
        $GoodsSpecModel= new GoodsSpecModel();
        $s=[];
        $s[]=['name','=',$name];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['is_delete','=',0];
        $all_info=$GoodsSpecModel->where($s)->find();
        if(!empty($all_info)) return $this->error('数据重复');
        foreach ($spec_text as $k=>&$va){
            $va['sort']=$k+1;
        }
        $spec_text=json_encode($spec_text,JSON_UNESCAPED_UNICODE);
        $insert=compact('shop_id','name','spec_text');
        $insert['add_time']=date('Y-m-d H:i:s');
        $insert['edit_time']=date('Y-m-d H:i:s');
        $insert_id=GoodsSpecModel::insertGetId($insert);
        return $this->success($insert_id);
    }

}

